import 'package:flutter/material.dart';
import 'package:flutter_test_provider/providerDemo/counter_provider.dart';
import 'package:flutter_test_provider/providerDemo/second_counter_page.dart';
import 'package:provider/provider.dart';

class ProviderOfPage extends StatefulWidget {
  @override
  _ProviderOfPageState createState() => _ProviderOfPageState();
}

class _ProviderOfPageState extends State<ProviderOfPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Provider.Of-demo"),
      ),
      body: ChangeNotifierProvider<CounterProvider>(
        create: (_) => CounterProvider(),
        child: Center(
          child: Column(
            children: [
              CounterWidget(),
            ],
          ),
        ),
      ),

      // floatingActionButton: Consumer<CounterProvider>(
      //   builder: (ctx, counterPro, child) {
      //     return FloatingActionButton(onPressed: () {
      //       counterPro.counter += 1;
      //       print("点击响应");
      //     });
      //   },
      // ),
    );
  }

  void pushToSecondPage() {
    Navigator.push(context, MaterialPageRoute(builder: (context) {
      return SecondPage();
    }));
  }
}

class CounterWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    print("-----------------CounterWidget-build------------");
    return Column(
      children: [
        Container(
          child: Text(Provider.of<CounterProvider>(context).counter.toString()),
        ),
        Consumer<CounterProvider>(builder: (ctx, counterPro, child) {
          return RaisedButton(
              child: Text("+1"),
              onPressed: () {
                counterPro.counter += 1;
                print("点击响应");
              });
        }),
      ],
    );
  }
}
